var util = require('../../utils/util.js')
var netApi = require('../../net/api.js');
var loadingApi = require('../../ui/loadingPage.js');
var baseObj;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //主体内容隐藏
    contentIsHide: true,
    //轮播图 
    ads: [],
    //医生热门
    docHot: [],
    //医生推荐
    docRecommend: [],
    scan: "../../" + util.imagePath("scan.png"),
    msg: "../../" + util.imagePath("msg.png"),
    serch: "../../" + util.imagePath("serach.png"),
    //'loadingJS'与 loadingPage.wxml 关联
    loadingJS: loadingApi,
    //医生默认头像
    docHead: "../../" + util.imagePath("default_head_doc.png"),
  },

  /**enablePullDownRefresh
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    baseObj = this;
    loadingApi.loadingShow(this);
    this.loadCacheData();
    this.reqBanner();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉刷新");
    wx.showNavigationBarLoading();
    this.reqBanner();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("onReachBottom");
  },
  onScrollRefresh: function () {
    //scroll-view 滑动到顶部
    console.log("scroll-view 滑动到顶部");

  },
  onScroll: function (eventHandle) {
    //scroll-view 的滑动
    //console.log(eventHandle);
  },

  onScrollBottom: function () {
    //scroll-view 滑动到底部
    console.log("scroll-view 滑动到底部");
  },
  /**数据缓存 和 设置
   * isSave true:保存
   * docHot :热门医生
   * docRecommend :医生推荐
   */
  loadCacheData: function (isSave, ads, docHot, docRecommend) {
    if (isSave) {
      wx.setStorage({
        key: "ads",
        data: ads
      }),
        wx.setStorage({
          key: "docHot",
          data: docHot
        }),
        wx.setStorage({
          key: "docRecommend",
          data: docRecommend
        })
      //加载成功
      loadingApi.loadingSucceed(baseObj);
      this.setData({ contentIsHide: false, ads: ads, docHot: docHot, docRecommend: docRecommend });
      return;
    }
    var self = this;
    //缓存的轮播图
    wx.getStorage({
      key: 'ads',
      success: function (res) {
        var ads = res.data;
        //加载成功
        loadingApi.loadingSucceed(baseObj);
        self.setData({ contentIsHide: false, ads: ads });
      },

    }),
      //缓存的医生列表
      wx.getStorage({
        key: 'docHot',
        success: function (res) {
          var docHot = res.data;
          self.setData({ docHot: docHot });
        }
      }),
      wx.getStorage({
        key: 'docRecommend',
        success: function (res) {
          var docRecommend = res.data;
          self.setData({ docRecommend: docRecommend });
        }
      })
  },
  //重新开始请求
  loadingRest: function () {
    loadingApi.loadingShow(this);
    this.reqBanner();
  },
  //获取轮播图 和 医生列表
  reqBanner: function () {
    var netBack = this.netBack;
    var reqServe = netBack.reqServe[0];
    var mapObj = netApi.getRequestParameters(reqServe);
    var json = netApi.mapToJson(mapObj);
    netApi.wxRequest(mapObj, this.netBack, reqServe);
  },
  //网络请求回调
  netBack: {
    //请求service列表
    reqServe: ['nethos.system.app.pat.index'],
    //对应的请求service返回成功和失败的列表，奇数成功，偶数失败
    reqState: [[1, 2]],
    //获取自定义状态码
    getReqState: function (tag) {
      var state = [1, 2];
      for (var i = 0; i < this.reqServe.length; i++) {
        if (this.reqServe[i] == tag) {
          state = this.reqState[i];
          break
        }
      }
      return state;
    },
    //请求结果回调
    onBack: function (what, obj, msg) {
      switch (what) {
        case 1:
          console.log("业务处理成功，" + msg);
          var ads = obj.sysAdSettingList;
          var docHot = obj.picDocList;
          var docRecommend = obj.videoDocList;
          baseObj.loadCacheData(true, ads, docHot, docRecommend);
          break;
        case 2:
          console.log("业务处理失败，" + msg);
          loadingApi.loadingFail(baseObj);
          break;
      }
    },
    //请求完成
    complete: function (res, tag) {
      console.log("请求完成:" + tag);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    },
  }
})
